import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class Project {
    public static void main(String[] args){
        Check1 check1 = new Check1();
        Check2 check2 = new Check2();

    }
}

/*
选择框
JCheckBox
    可以勾选多个，自定义一个类，实现接口ItemListener的itemStateChanged方法
    1.创建对象,可初始化状态
        JCheckBox jbox1 = new JCheckBox("box1",true);
    2.为选择框注册事件，实现功能
        jbox1.addItemListener(this);
        public void itemStateChanged(ItemEvent e) {//实现接口方法
        if(e.getItemSelectable() == jbox1){//获取事件源，判断是哪个选择框发生变化
            if (e.getStateChange() == ItemEvent.SELECTED)//获取选择框状态
                System.out.println("jbox1 selected");
            else
                System.out.println("jbox1 cancel");
        }
 */
class Check1 extends JFrame implements ItemListener {
    JCheckBox jbox1, jbox2, jbox3;

    Check1() {
        jbox1 = new JCheckBox("box1",true);
        jbox2 = new JCheckBox("box2");
        jbox3 = new JCheckBox("box3");
        Container con = getContentPane();
        con.setLayout(new GridLayout());
        con.add(jbox1);
        con.add(jbox2);
        con.add(jbox3);

        setSize(300,200);
        setVisible(true);

        jbox1.addItemListener(this);
        jbox2.addItemListener(this);
        jbox3.addItemListener(this);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getItemSelectable() == jbox1){
            if (e.getStateChange() == ItemEvent.SELECTED)
                System.out.println("jbox1 selected");
            else
                System.out.println("jbox1 cancel");
        }else if(e.getItemSelectable() == jbox2){
            if (e.getStateChange() == ItemEvent.SELECTED)
                System.out.println("jbox2 selected");
            else
                System.out.println("jbox2 cancel");
        }else if(e.getItemSelectable() == jbox3){
            if (e.getStateChange() == ItemEvent.SELECTED)
                System.out.println("jbox3 selected");
            else
                System.out.println("jbox3 cancel");
        }
    }
}

/*
单选框
JRadioButton
    只可勾选一个，自定义一个类，实现接口ItemListener的itemStateChanged方法
    1.创建多个JRadioButton对象,可初始化状态，
    将这些按键添加到ButtonGroup按键组中，可实现只选中一个
        JRadioButton jrd1, jrd2, jrd3;
        ButtonGroup g1;
        g1.add(jrd1);
        g1.add(jrd2);
        g1.add(jrd3);

    2.为选择框注册事件，实现功能
    通过getItem可获取到是哪个按键在触发
    通过getStateChange获取该按键的状态

       jrd1.addItemListener(this);
       public void itemStateChanged(ItemEvent e) {//实现接口方法
       if(e.getItem() == jrd1){
            if (e.getStateChange() == ItemEvent.SELECTED) {
                System.out.println("jrd1");
            }
        }
 */
class Check2 extends JFrame implements ItemListener {
    JRadioButton jrd1, jrd2, jrd3;
    ButtonGroup g1;
    Check2() {
        jrd1 = new JRadioButton("jrd1",true);
        jrd2 = new JRadioButton("jrd2",false);
        jrd3 = new JRadioButton("jrd3",false);
        g1 = new ButtonGroup();
        g1.add(jrd1);
        g1.add(jrd2);
        g1.add(jrd3);

        Container con = getContentPane();
        con.setLayout(new GridLayout());
        con.add(jrd1);
        con.add(jrd2);
        con.add(jrd3);

        setSize(300, 200);
        setVisible(true);

        jrd1.addItemListener(this);
        jrd2.addItemListener(this);
        jrd3.addItemListener(this);
    }
    public void itemStateChanged(ItemEvent e) {
        if(e.getItem() == jrd1){
            if (e.getStateChange() == ItemEvent.SELECTED) {
                System.out.println("jrd1");
            }
        }else if(e.getItem() == jrd2){
            if (e.getStateChange() == ItemEvent.SELECTED) {
                System.out.println("jrd2");
            }
        }else if(e.getItem() == jrd3){
            if (e.getStateChange() == ItemEvent.SELECTED){
                System.out.println("jrd3");
            }
        }
    }
}